import 'dart:convert' show json;

import 'package:flutter/cupertino.dart';
import 'package:mahjong/entity/UserEntityInfo.dart';

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class MetaUserChannel with ChangeNotifier {
  String userId;
  bool banker;
  int seq;
  bool win;
  bool online;
  SysUser sysUser;

  MetaUserChannel(
      {this.userId,
      this.banker,
      this.seq,
      this.win,
      this.online,
      this.sysUser});

  Map<String, dynamic> toJson() => <String, dynamic>{
        'userId': userId,
        'banker': banker,
        'seq': seq,
        'win': win,
        'online': online,
        'sysUser': sysUser
      };

  factory MetaUserChannel.fromJson(Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : MetaUserChannel(
              userId: asT<String>(jsonRes['userId']),
              banker: asT<bool>(jsonRes['banker']),
              seq: asT<int>(jsonRes['seq']),
              win: asT<bool>(jsonRes['win']),
              online: asT<bool>(jsonRes['online']),
              sysUser: SysUser.fromJson((jsonRes)['sysUser']),
            );

  @override
  String toString() {
    return json.encode(this);
  }
}
